Skip to content

gh-135797: Add _field_types to abstract AST classes - #159567

Open
Wojusensei wants to merge 1 commit into
python:mainfrom
Wojusensei:gh-135797
Open

Wojusensei wants to merge 1 commit into
python:mainfrom
Wojusensei:gh-135797

Conversation

@Wojusensei

@Wojusensei Wojusensei commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Classes without fields have always had an empty _fields tuple, but they never got a _field_types dictionary, so the attribute was missing on ast.AST and on the abstract classes such as ast.mod, while typeshed declares it on ast.AST. make_type() now sets an empty dictionary on every class it creates, and ast.AST gets one too

the constructor used the missing attribute to tell user subclasses that declare _fields but no _field_types apart from the built-in nodes. an empty dictionary is handled the same way now, so those subclasses keep the behavior they have had before 3.13; without that, class Fields(ast.AST): _fields = ('a',) raises TypeError: Field 'a' is missing from Fields._field_types.

Lib/test/test_ast/test_ast.py gets a test checking that every class in the ast module has a _field_types dictionary whose keys match _fields, and test_fields_but_no_field_types covers the subclass behavior above. test_ast passes, also with -R 3:3

@read-the-docs-community

read-the-docs-community Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #35204027 | 📁 Comparing 711a776 against main (d3ada45)

  🔍 Preview build  

4 files changed
± library/ast.html
± library/calendar.html
± library/test.html
± whatsnew/changelog.html

Classes without fields never got a _field_types dictionary, so the
attribute was missing on ast.AST and on the abstract classes such as
ast.mod, while typeshed declares it on ast.AST.  make_type() now sets an
empty dictionary on every class it creates, and ast.AST gets one too;
add_ast_annotations() keeps filling in the concrete classes.

The constructor relied on the missing attribute to detect user subclasses
that declare _fields but no _field_types, and those must keep working as
before 3.13.  An empty dictionary is treated the same way now, otherwise
they would start failing with a missing-field TypeError.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant